Ext.define('TS.view.common.BillHistoryBrowseDialog', {
    extend: 'Ext.window.Window',
    alias : 'widget.billhistorybrowsedialog',
	mixins: {
        observable: 'Ext.util.Observable'
    },
    constructor: function () {
    	this.callParent();
        this.addEvents(
        {'selected':true}
        );
    },
    title : '历史查询',
    width:900,
    layout:'fit',
    height:400,
    config: {
    	custId:-1,//设置初始查询条件
    	productId:-1,
    	begDate:new Date(),
    	endDate:new Date(),
    	isSale:true
    },
    modal:true,
    initComponent: function() {
    	Ext.apply(this, {
    		tbar :[
				{
					xtype:'datefield',
	        	 	name:'begDate',
	        	 	labelWidth:50,
	        	  	fieldLabel:'起始日期',
	        	  	format:'Y-m-d'
				},
				{
					xtype:'datefield',
	        	 	name:'endDate',
	        	 	labelWidth:50,
	        	  	fieldLabel:'结束日期',
	        	  	format:'Y-m-d'
				},
				{xtype: 'button',text:'查询',tabIndex:4,margin:'0 0 0 10',action:'search',iconCls:'bill-search-button',
					listeners: {
						click:{
							fn:function(btn,e){
								var win=btn.up("window");
								var custid=win.getCustId();
								var pid=win.getProductId();
								var isSale=win.getIsSale();
								var begdate=Ext.Date.format(win.down('datefield[name=begDate]').getValue(),'Y-m-d');
								var enddate=Ext.Date.format(win.down('datefield[name=endDate]').getValue(),'Y-m-d');
								var grid=win.items.items[0];
								var url="billhis/salehistory.do";
								if(!isSale)
									url="billhis/purchasehistory.do"
								if(!Ext.isEmpty(begdate)){
									grid.store.load({
										url:url,
										params:{begDate:begdate,endDate:enddate,productId:pid,custId:custid},
							    		callback:function(records, operation, success){
							    			if(records){
								    			if(records.length>0){
								    				//多条记录默认选择第一条
								    				grid.getSelectionModel().select(records[0],true);
								    			}
							    			}
							    		},
								   		scope: this
									});					
								}
							}
						}
					}
				}
    		],
    		items:[
        		Ext.create('TS.view.common.BillHistorySimpleList',{
        			autoScroll: true,
        			title:'',
        			closable: false,
        			listeners: {
        				//双击
						itemdblclick:{
							fn:function(grid, record, item, index, e, eOpts){
								var records=[];
								records.push(record);
								var win=this.up('window');
								win.fireEvent('selected',records);
	                			win.hide();
							}
						},
						//回车
						cellkeydown:{
							fn:function(grid, td, cellIndex, record, tr, rowIndex, e, eOpts ){
								if(e.keyCode==e.ENTER){
									var records=[];
									records.push(record);
									var win=this.up('window');
									win.fireEvent('selected',records);
		                			win.hide();
								}
							}
						}
					}        			
        		})
        	],
        	buttons:[
	        	{
	                text: '确认',
	                scope: this,
	                handler:function(){
	                	var grid=this.items.items[0];
	                	var records=grid.getSelectionModel().getSelection();
	                	//确认没有选择行则不执行.
	                	if(records.length>0){
	                		this.fireEvent('selected',records);
	                		this.hide();
	                	}
	                	
	                }
	            },
	            {
	                text: '取消',
	                scope: this,
	                handler: this.close
	            }
	        ]
    	});

        this.callParent(arguments);
    },
	listeners: {
		render:{
			fn:function(){
				var grid=this.items.items[0];
				var custId=this.getCustId();
				var prodId=this.getProductId();
				
				var begdate=Ext.Date.format(this.getBegDate(),'Y-m-d');
				var enddate=Ext.Date.format(this.getEndDate(),'Y-m-d');
				var isSale=this.getIsSale();
				var url="billhis/salehistory.do";
				if(!isSale)
					url="billhis/purchasehistory.do"
				if(!Ext.isEmpty(begdate)){
					grid.store.load({
						//actionMethods:{read:'POST'},
						
						url:url,
						params:{begDate:begdate,endDate:enddate,productId:prodId,custId:custId},
			    		callback:function(records, operation, success){
			    			this.down("datefield[name=begDate]").setValue(this.getBegDate());
							this.down("datefield[name=endDate]").setValue(this.getEndDate());
			    			if(records){
				    			if(records.length>0){
				    				//多条记录默认选择第一条
				    				grid.getSelectionModel().select(records[0],true);
									
				    			}
			    			}
			    		},
				   		scope: this
					});					
				}
			}
		},
		show:{
			fn:function(){
				var grid=this.items.items[0];
				//如有数据默认选择第一行并设置焦点为Grid,支持上下键操作和回车,没有数据焦点停在条件输入框
				if(grid.store.getTotalCount()>0){
					grid.focus(100);
				}else{
					//var txtfield=this.down('triggerfield');
					//txtfield.focus('',100);
				}
				
			}
		}
	}
});